﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JChatData.Helper;
using System.IO;
using JChatSerVices.GetConnect;
using JChatSerVices.UserService;
using JChatSerVices.CityService;
using JChatSerVices.CountryService;
using JChatCore.User;
using JChatAdmin.Models;

namespace JChatAdmin.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /User/
        GetFileConnect _get;
        DBConectString _db;
        UserService _user;
        public UserController()
        {
            _user = new UserService();
            _get = new GetFileConnect();
            string file = _get.GetFile("conn");
            _db = new DBConectString(file);
        }
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult UserManagement()
        {
            var user = this._user.GetAllUser();
            ViewBag.data = user;
            return View("UserManagement");
        }
        public ActionResult Delete(Guid id)
        {
            this._user.DeleteUser(id);
            return RedirectToAction("UserManagement", "User");
        }
        public ActionResult Create()
        {
            var City = (from c in _db._context.JChat_City
                        select c).ToList();
            var Country = (from c in _db._context.JChat_CounTry
                           select c).ToList();
            var Sex = new List<SelectListItem>{new SelectListItem{Text = "Male", Value = "Male"}, new SelectListItem{Text = "Female", Value = "Female"}};
            JChatAdmin.Models.UserModel.UserModel _umodel = new Models.UserModel.UserModel();
            _umodel.SelectListCity = new SelectList(City, "CityID", "NameCity");
            _umodel.SelectListCountry = new SelectList(Country, "CountryID", "NameCountry");
            _umodel.SelectSex = new SelectList(Sex, "Value", "Text");
            return View("Create", _umodel);
        }
        [HttpPost]
        public ActionResult Create(User user)
        {
            if (_user.AddUser(user))
            {
                return RedirectToAction("UserManagement", "User");
            }
            else
            {
                System.Web.HttpContext.Current.Session["h"] = "loi";
                return RedirectToAction("Create", "User");
            }
        }
        public ActionResult Edit(string id)
        {
            Guid ID = new Guid(id);
            var _u = _user.GetUserById(ID);
            var City = (from c in _db._context.JChat_City
                        select c).ToList();
            var Country = (from c in _db._context.JChat_CounTry
                           select c).ToList();
            var Sex = new List<SelectListItem> { new SelectListItem { Text = "Male", Value = "Male" }, new SelectListItem { Text = "Female", Value = "Female" } };
           JChatCore.User.User u = new User();
           
           foreach (JChatCore.User.User a in _u)
           {
               u.RealName = a.RealName;
               u.Email = a.Email;
               u.Address = a.Address;
               u.Status = a.Status;
               u.SelectListCity = new SelectList(City, "CityID", "NameCity");
               u.SelectListCountry = new SelectList(Country, "CountryID", "NameCountry");
               u.SelectSex = new SelectList(Sex, "Value", "Text");
               u.BirthDay = a.BirthDay;
               u.Avatar = a.Avatar;
               u.CreateDate = a.CreateDate;
               u.OtherInFo = a.OtherInFo;
               u.Active = a.Active;
           }
            return View("Edit",u);
        }
        [HttpPost]
        public ActionResult Edit(Guid id, User user)
        {
            if (_user.UpdateUser(id, user))
                return RedirectToAction("UserManagement", "User");
            else
                return RedirectToAction("Edit", "User");
        }
    }
}
